package whf.framework.web.tag;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.jsp.JspException;

import whf.framework.util.StringUtils;

/**
 * @author wanghaifeng
 * @create Aug 12, 2006 12:10:05 AM 使用click事件响应的树控件
 * 
 */
public class TreeTag2 extends HtmlTag {
	
	/**
	 * 根结点id
	 */
	private long rootId;
	
	/**
	 * 服务名称
	 */
	private String serviceName;
	
	/**
	 * 过滤字符串
	 */
	private String queryString;
	
	/**
	 * xml路径
	 */
	private String xmlPath;

	/**
	 * 树名称
	 */
	private String treeName;

	/**
	 * 选择类型radio, checkbox等
	 */
	private String selectType;

	private boolean selectAll;

	/**
	 * 选中的id
	 */
	private String checkedId;

	private boolean expand;
	
	private boolean lazy;

	public void setRootId(long rootId) {
		this.rootId = rootId;
	}

	public void setServiceName(String serviceName) {
		this.serviceName = serviceName;
	}
	
	public void setQueryString(String query){
		this.queryString = query;
	}

	public void setCheckedId(String checkedId) {
		this.checkedId = checkedId;
	}

	public void setSelectAll(boolean selectAll) {
		this.selectAll = selectAll;
	}

	public void setSelectType(String selectType) {
		this.selectType = selectType;
	}

	public void setTreeName(String treeName) {
		this.treeName = treeName;
	}

	public void setExpand(boolean expand) {
		this.expand = expand;
	}

	public void setXmlPath(String xmlPath) {
		this.xmlPath = xmlPath;
	}
	
	public void setLazy(boolean b){
		this.lazy = b;
	}
	
	private String getXmlPath(){
		String result = this.xmlPath;
		if(StringUtils.isEmpty(result)){
			result = this.request().getContextPath() + "/servlet/TreeXml.xml?lazy="+this.lazy+"&service=" + this.serviceName + 
				"&root=" + this.rootId + "&onlyroot="+(this.rootId > 0);
			if(!StringUtils.isEmpty(queryString)){
				result += "&queryString=" + this.queryString;
			}
		}
		if(!StringUtils.isEmpty(checkedId)){
			if(result.indexOf("?") < 0){
				result += "?";
			}
			if(!result.endsWith("?") && !result.endsWith("&")){
				result += "&";
			}
			result += "checkedId=" + this.checkedId;
		}
		return result;
	}

	public int doStartTag() throws JspException {
		HttpServletResponse response = (HttpServletResponse) pageContext.getResponse();
		response.setHeader("Pragma", "No-cache");
		response.setHeader("Cache-Control", "no-cache");
		response.setHeader("Expires", "0");
		response.setDateHeader("Expires", (new java.util.Date(0)).getTime());

		outputWebFxTree();
		return SKIP_BODY;
	}

	public int doEndTag() throws JspException {
		this.release();
		super.flush();
		return EVAL_PAGE;
	}

	// WebFxTree
	private void outputWebFxTree() throws JspException {
		HttpServletRequest request = (HttpServletRequest) pageContext.getRequest();
		println("<script src=\"" + request.getContextPath() + "/scripts/tree/jsre.js\"></script>");

		println("<script  language=\"javascript\">");

		println("_import(\"com.eae.webfx.xtree.WebFXLoadTree\");");
		println("_import(\"com.eae.webfx.xtree.WebFXTree\");");
		println("_import(\"com.eae.webfx.xtree.WebFXTreeItem\");");
		println("_import(\"com.eae.webfx.xtree.WebFXUtil\");");
		println("_import(\"com.eae.webfx.xtree.WebFXTreeHandler\");");
		println("</script>");

		println("<script type=\"text/javascript\">");

		println("var atree = new WebFXLoadTree(\"" + (treeName == null?"Root":treeName) + "\",\"" + this.getXmlPath() + "\");");

		println("atree.build()");
		if (this.selectType != null) {
			println("WebFXUtil.setSelectType(\"" + this.selectType + "\");");
		}
		println("WebFXUtil.setSelectAll(" + this.selectAll + ");");
		println("atree.onCheck = function (node, tree)");
		println("{");
		println("\tnode.checkit();");
		println("}");
		println("function selectNode(){}");
		println("\tvar expandFlag = false;");
		println("document.ondblclick=function(){");
		println("\tif(!expandFlag){");
		println("\t\ttry{atree.expandAll();}catch(e){}");
		println("\t\texpandFlag = true;");
		println("\t}else{");
		println("\t\ttry{atree.collapseAll();}catch(e){}");
		println("\t\texpandFlag = false;");
		println("\t}");
		println("}");
		if (this.expand) {
			println("var timer;\r\n");
			println("function lookup(){\r\n");
			println("\tif(timer)clearTimeout(timer);\r\n");
			println("\ttimer=setTimeout(goLookup,100);\r\n");
			println("}\r\n");
			println("function goLookup(){\r\n");
			println("\tatree.expandAll();\r\n");
			println("\texpandFlag = true;\r\n");
			println("}\r\n");
			println("lookup();\r\n");

			println("window.onerror = function fnErrorTrap(sMsg,sUrl,sLine){\r\n");
			println("if(sMsg == \"由于该控件目前不可见、未启用或其类型不允许，因此无法将焦点移向它。\"){\r\n");
			println("return true;\r\n");
			println("}\r\n");
			println("return false;\r\n");
			println("}\r\n");
		}
		println("</script>");

	}

	public void release() {
		super.release();
		this.serviceName = null;
		this.rootId = 0;
		this.treeName = null;
	}

}